package com.app.controller;

import com.app.model.CustomerModel;
import com.app.model.JobPublishModel;
import com.app.model.PositionModel;
import com.app.model.UserModel;
import com.app.service.*;
import com.app.util.*;
import com.app.vo.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;


@Controller
@RestController
public class CustomerController {

    @Autowired
    CustomerService customerService;
    @Autowired
    UserService userService;
    @Autowired
    ComRecruitersService comRecruitersService;
    @Autowired
    JobPublishService jobPublishService;
    @Autowired
    JobIntentionService jobIntentionService;
    @Autowired
    CusEducationService cusEducationService;
    @Autowired
    CusWorkExperienceService cusWorkExperienceService;
    @Autowired
    PositionService positionService;

    /**
     * 根据token查询用户
     */
    @GetMapping("/getCustomer")
    public Result<Object> getCustomer(HttpServletRequest request, @RequestParam(required = false) Integer id) throws ParseException {
        String token = request.getHeader("token");
        return Result.success(customerService.getCustomer(token, id));
    }

    @GetMapping("/getUserCustomer")
    public Result<Object> getUserCustomer(HttpServletRequest request, @RequestParam(required = false) Integer id) throws ParseException {
        String token = request.getHeader("token");
        return Result.success(customerService.getUserCustomer(token, id));
    }

    @PostMapping("/toUpdateCustomer")
    public Result<Object> addUser(@RequestParam("realName") String realName,
                                  @RequestParam("birthday") String birthday,
                                  @RequestParam("sex") Integer sex,
                                  @RequestParam(required = false) String eml,
                                  @RequestParam(required = false) String weChat,
                                  @RequestParam("faceUrl") String faceUrl,
                                  @RequestParam("jobStatus") Integer jobStatus,
                                  @RequestParam(required = false) String passWord,
                                  @RequestParam(required = false) Integer streetId,
                                  HttpServletRequest request) {
        String token = request.getHeader("token");
        String mobile = TokenUtils.nothingness(token);
        if (mobile == null) {
            return Result.error(405, "token不存在");
        }
        UserModel user = userService.getUserByMobile(mobile);
        CustomerModel cus = customerService.getCustomerById(user.getCId());
        if (cus == null) {
            cus = new CustomerModel();
        }
        if (streetId != null && streetId > 0) {
            cus.setStreetId(streetId);
        }
        cus.setRealName(realName);
        cus.setBirthday(birthday);
        cus.setSex(sex);
        cus.setEml(eml);
        cus.setWeChat(weChat);
        cus.setJobStatus(3);
        cus.setJobStatus(jobStatus);
        cus.setCrtTime(CommonHelp.getNowTimestamp());
        if (cus.getId() > 0) {
            customerService.update(cus);
        } else {
            customerService.insert(cus);
        }
        int cusId = cus.getId();
        if (user == null) {
            user = new UserModel();
        }
        user.setCId(cusId);
        user.setCRegisterState(1);
        user.setFaceUrl(faceUrl);
       if(StringTool.isNotEmpty(passWord))
       {
           String keyWord = AESTool.dealAESEncrypt(passWord,"szjy");
           user.setPassWord(keyWord);
       }
        userService.update(user);
        user.setCus(cus);

        UserModel userModel = new UserModel();
        userModel.setRealName(user.getRealName());
        userModel.setFaceUrl(user.getFaceUrl());
        userModel.setMobile(user.getMobile());
        userModel.setResumeStatus(user.getUserMsg());
        userModel.setInfoStatus(user.getInfoStatus());
        LoginVo loginVo = new LoginVo();
        loginVo.setToken(token);
        loginVo.setUser(userModel);
        return Result.success(loginVo);
    }

    @GetMapping("/getCustomerList")
    public Result<Object> getCustomerList(
            @RequestParam(required = false) Integer recruitType,//招聘类型
            @RequestParam(required = false) Integer salaryId,//薪资id
            @RequestParam(required = false) String eduId,//学历id
            @RequestParam(required = false) Integer expId,//经验id
            @RequestParam(required = false) Integer sex,//性别
            @RequestParam(required = false) Integer jobType,//行业
            @RequestParam(required = false) String jobTypes,//多行业
            @RequestParam(required = false) Integer jobLevel,//行业级别
            @RequestParam(required = false) String searchName,//关键字
            Integer page,
            Integer limit,
            HttpServletRequest request) throws ParseException {
        String token = request.getHeader("token");
        UserModel user = null;
        if (StringTool.isNotEmpty(token)) {
            String mobile = TokenUtils.verify(token);
            user = userService.getUserByMobile(mobile);
            if (user == null) {
                return Result.error(405, "用户不存在");
            }
        }


        CustomerModel cus = new CustomerModel();
        cus.setSearchName(searchName);
        cus.setRecruitType(recruitType);
        cus.setSalaryId(salaryId);
        if (StringTool.isNotEmpty(eduId)) {
            List<Integer> eduIdList = new ArrayList<>();
            String[] strs = eduId.split(",");
            for (int i = 0; i < strs.length; i++) {
                eduIdList.add(Integer.valueOf(strs[i]));
            }
            cus.setEduIds(eduIdList);
        }
        cus.setExpId(expId);
        cus.setSex(sex);
        cus.setPage((page - 1) * limit);
        cus.setLimit(limit);
        cus.setJobType(jobType);
        if (StringTool.isNotEmpty(jobTypes) && !jobTypes.equals("0")) {
            List<Integer> jobtypeList = new ArrayList<>();
            String[] strs = jobTypes.split(",");
            for (int i = 0; i < strs.length; i++) {
                jobtypeList.add(Integer.valueOf(strs[i]));
            }
            cus.setJobTypes(jobtypeList);
        }

        cus.setLevel(jobLevel);
        if (jobLevel != null && jobType != null && jobLevel == 2) {
            PositionModel positionModel = positionService.getModelById(jobType);
            cus.setFatherCode(positionModel.getPostCode());
        }
        JobPublishModel jobPublish = new JobPublishModel();
        if (user != null) {
            jobPublish.setRecId(user.getRId());
        }
        jobPublish.setRecruitState(3);
        jobPublish.setRecruitType(1);
        jobPublish.setPage(0);
        jobPublish.setLimit(100);
        List<JobPublishModel> modelList = jobPublishService.getJobPublishListByModel(jobPublish);
        if (modelList != null && modelList.size() > 0) {
            cus.setPostId(modelList.get(0).getJobType());
        }
        if (StringTool.isEmpty(cus.getJobText())) {
            cus.setJobText("暂无状态");
        }
        return Result.success(customerService.getCusAndIntentionList(cus));
    }

    @GetMapping("/getBackstageCustomerList")
    public Result<Object> getBackstageCustomerList(
            @RequestParam(required = false) Integer recruitType,//招聘类型
            @RequestParam(required = false) Integer salaryId,//薪资id
            @RequestParam(required = false) String eduId,//学历id
            @RequestParam(required = false) Integer expId,//经验id
            @RequestParam(required = false) Integer sex,//性别
            @RequestParam(required = false) Integer jobType,//行业
            @RequestParam(required = false) String jobTypes,//多行业
            @RequestParam(required = false) Integer jobLevel,//行业级别
            @RequestParam(required = false) String keyWord,//关键字
            Integer page,
            Integer limit,
            HttpServletRequest request) throws ParseException {
        CustomerModel cus = new CustomerModel();
        cus.setRecruitType(recruitType);
        cus.setSalaryId(salaryId);
        if (StringTool.isNotEmpty(eduId)) {
            List<Integer> eduIdList = new ArrayList<>();
            String[] strs = eduId.split(",");
            for (int i = 0; i < strs.length; i++) {
                eduIdList.add(Integer.valueOf(strs[i]));
            }
            cus.setEduIds(eduIdList);
        }
        cus.setExpId(expId);
        cus.setSex(sex);
        cus.setKeyword(keyWord);
        cus.setPage((page - 1) * limit);
        cus.setLimit(limit);
        cus.setJobType(jobType);
        if (StringTool.isNotEmpty(jobTypes) && !jobTypes.equals("0")) {
            List<Integer> jobtypeList = new ArrayList<>();
            String[] strs = jobTypes.split(",");
            for (int i = 0; i < strs.length; i++) {
                jobtypeList.add(Integer.valueOf(strs[i]));
            }
            cus.setJobTypes(jobtypeList);
        }

        cus.setLevel(jobLevel);
        if (jobLevel != null && jobType != null && jobLevel == 2) {
            PositionModel positionModel = positionService.getModelById(jobType);
            cus.setFatherCode(positionModel.getPostCode());
        }
        JobPublishModel jobPublish = new JobPublishModel();

        jobPublish.setRecruitState(3);
        jobPublish.setRecruitType(1);
        jobPublish.setPage(0);
        jobPublish.setLimit(100);
        List<JobPublishModel> modelList = jobPublishService.getJobPublishListByModel(jobPublish);
        if (modelList != null && modelList.size() > 0) {
            cus.setPostId(modelList.get(0).getJobType());
        }
        if (StringTool.isEmpty(cus.getJobText())) {
            cus.setJobText("暂无状态");
        }
        return Result.success(customerService.getCusAndIntentionList(cus));
    }

    //修改求职状态
    @PostMapping("/toUpdateCustomerStatus")
    @ResponseBody
    public Result<Object> toUpdateCustomerStatus(@RequestParam("jobStatus") int jobStatus,
                                                 HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String mobile = TokenUtils.verify(token);
        UserModel user = userService.getUserByMobile(mobile);
        if (user == null) {
            return Result.error(405, "用户不存在");

        }
        CustomerModel cus = customerService.getCustomerById(user.getCId());
        cus.setJobStatus(jobStatus);
        cus.setUpTime(CommonHelp.getNowTimestamp());
        if (jobStatus > 0) {
            customerService.update(cus);
        } else {
            return Result.error(201, "ID不正确");
        }
        user.setCus(cus);
        LoginVo vo = new LoginVo();
        vo.setUser(user);
        vo.setToken(token);
        return Result.success(vo);
    }
}
